#include "DGGSPoint.hpp"
#include <vector>
using namespace NNU::DGGS::Base;
using  namespace NNU::DGGS::Feature;

DGGSPoint *DGGSPoint::deepCopy() {
    DGGSPoint *newPoint = new DGGSPoint();
    newPoint->_gridCount=this->_gridCount;
    newPoint->setGridData(this->_gridData);
    return newPoint;
}

bool DGGSPoint::Equals( DGGSGeometry *other) {
    if(other->getShapeType()!=ST_Point)
        return false;
    else
    {
        DGGSPoint *oPoint = dynamic_cast<DGGSPoint *>(other);

        if(this->_gridCount!=oPoint->getGridCount())
            return false;

        //比较两要素是否相等
        DGGSElementCode* otherData= nullptr;
        other->getGridData(otherData);
        DGGSElementCode* srcData= nullptr;
        this->getGridData(srcData);

        if(otherData== nullptr||srcData== nullptr)
            return false;

        for(int i=0;i<this->getGridCount();i++)
        {
            if (srcData[i] == otherData[i])
                continue;
            else
                return false;
        }
        delete[]otherData;
        delete[]srcData;
        return true;
    }
}

bool DGGSPoint::setGridData(DGGSElementCode *srcData) {
    if(srcData== nullptr)
        return false;
    //设置data
    this->_gridData=new DGGSElementCode[this->_gridCount];
    for(int i=0;i<this->_gridCount;i++)
        this->_gridData[i]=srcData[i];
    return true;
}

void DGGSPoint::addGridCode(DGGSElementCode srcCode)
{
    //设置data
    for(int i=0;i<this->_gridCount;i++)
        if(this->_gridData[i].level==-1)
            this->_gridData[i]= srcCode;
}

void DGGSPoint::setGridCount(int gridCount) {
    this->_gridCount=gridCount;
    this->_gridData = new DGGSElementCode[this->_gridCount];
    for (int i = 0; i < this->_gridCount; i++)
        this->_gridData[i].level = -1;
}

bool DGGSPoint::getGridData(DGGSElementCode* &srcData) {
    if(this->_gridCount==0)
        return false;
   srcData=new DGGSElementCode[this->_gridCount];
    for(int i=0;i<this->_gridCount;i++)
        srcData[i]=this->_gridData[i];
    return true;
}

DGGSElementCode DGGSPoint::getGridCodeByIndex(int index) {
    if (this->_gridCount == 0 || index > this->_gridCount || index < 0)
    {
        DGGSElementCode code;
        code.level = -1;
        return code;
    }
    else
        return this->_gridData[index];
}

int DGGSPoint::getGridCount() {
    return this->_gridCount;
}

DGGSPoint::~DGGSPoint()
{
    delete[]this->_gridData;
}

